package com.lcy.springtest.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

/**
 * @author lichenyang
 * @create 2021-04-16 4:11 下午
 */
@Component
public class Red implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {

    private ApplicationContext applicationContext;

    /**
     * BeanNameAware
     * 可以用来获取当前组件的名称
     * @param name: spring调用 传入bean的名称
     */
    @Override
    public void setBeanName(String name) {
        System.out.println("当前bean的名字 : " + name);
    }

    /**
     * ApplicationContextAware接口的实现
     * 可以获取当前spring的上下文信息
     * @param applicationContext: spring调用 传入ApplicationContext
     * @throws BeansException:
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        System.out.println("red 中 applicationContext:" + this.applicationContext);
    }

    /**
     * EmbeddedValueResolverAware 接口的实现
     * 可以用来解析字符串
     * @param stringValueResolver: spring调用 传入 stringValueResolver字符串解析器
     */
    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
        String s = stringValueResolver.resolveStringValue("你好${os.name} 我是 #{20*18}");
        System.out.println(s);
    }
}
